﻿using System;
using System.Collections.Generic;

using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using System.Collections;

namespace INSAForManagers
{
    class Xls
    {
        //엑셀 객체
        private Excel.Application oExcel = null;

        public Excel.Application OExcel
        {
            get { return oExcel; }
            set { oExcel = value; }
        }

        private Excel.Workbook oWorkBook;

        public Excel.Workbook OWorkBook
        {
            get { return oWorkBook; }
            set { oWorkBook = value; }
        }


        //워크 시트 객체
        private Excel.Worksheet oSheet;

        public Excel.Worksheet OSheet
        {
            get { return oSheet; }
            set { oSheet = value; }
        }

        //엑셀파일 이름
        private string xlsFileName;

        public string XlsFileName
        {
          get { return xlsFileName; }
          set { xlsFileName = value; }
        }

        
        //엑셀 열기
        public Excel.Worksheet openXls(string xls_name)
        {
            xlsFileName = xls_name;
            bool b = File.Exists(xls_name);
            if (b)
            {
                try
                {
                    OExcel = new Excel.ApplicationClass();                    
                    OExcel.DisplayAlerts = false;

                    OWorkBook = OExcel.Workbooks.Open(xlsFileName, 0, false, 5, Config.xls_password, Config.xls_password, false, Excel.XlPlatform.xlWindows, "", true, true, 0, true, false, false);

                    OSheet = (Excel.Worksheet)OWorkBook.Sheets[1];

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    OSheet = null;
                }
            }
            else {
                MessageBox.Show(xls_name + "프로그램의 구성 요소 중 일부가 삭제되거나 이동되었습니다.   ", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);               
            }

            return OSheet;

        }

        //엑셀 닫기
        public void closeXls()
        {
            if (OExcel != null)
            {                   
                OWorkBook.Close(false,Missing.Value, false);
                OExcel.Quit();                
                OExcel = null;
                
            }
        }

        public void changeSheet(int i)
        {
            oSheet = (Excel.Worksheet)OWorkBook.Sheets[i];
        }

    }
}
